package com.yonyou.gmmc.service.wechatpush.common;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpClientUtil {
	
private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
	
	/**
	*
	* httpclient-get
	* @date 2016年12月21日
	* @param url
	* @return
	*/
	public static String sendGetRequest(String url) {
        CloseableHttpClient client = null;
        CloseableHttpResponse res = null;
        try {
            client = HttpClients.createDefault();
            RequestConfig requestConfig = RequestConfig.custom().
                    setSocketTimeout(5000).setConnectTimeout(5000).build();//设置请求和传输超时时间
            
            //创建一个POST请求
            HttpGet httpGet = new HttpGet(url);
            httpGet.setConfig(requestConfig);
            // 执行get请求.    
            res = client.execute(httpGet);
            String resultStr = EntityUtils.toString(res.getEntity(), "UTF-8");
//            logger.info(resultStr);
            return resultStr;
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        } finally {
            if (res != null) {
                try {
                    res.close();
                } catch (IOException e) {
                	logger.error(e.getMessage(),e);
                }
            }
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                	logger.error(e.getMessage(),e);
                }
            }
        }
        return null;
	}
	
	/**
	*
	* httpclient-post
	* @date 2016年12月21日
	* @param url
	* @param jsonBody
	* @return
	*/
	public static String sendPostRequest(String url, String jsonBody){
        CloseableHttpClient client = null;
        CloseableHttpResponse res = null;
        try {
            client = HttpClients.createDefault();
            RequestConfig requestConfig = RequestConfig.custom().
                    setSocketTimeout(5000).setConnectTimeout(5000).build();//设置请求和传输超时时间
            
            //创建一个POST请求
            HttpPost post = new HttpPost(url);
            post.setConfig(requestConfig);
            StringEntity s = new StringEntity(jsonBody, "UTF-8");
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json;charset=UTF-8");
            post.setEntity(s);
            res = client.execute(post);
            logger.info("sendJsonMsgPost ok ");
            String resultStr = EntityUtils.toString(res.getEntity(), "UTF-8");
            logger.info(resultStr);
            return resultStr;
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            if (res != null) {
                try {
                    res.close();
                } catch (IOException e) {
                }
            }
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                }
            }
        }
        return null;
	}
	
    /**
    *
    * httpclient-put
    * @date 2016年12月22日
    * @param url
    * @param jsonBody
    * @return
    * @throws Exception
    */
    /*public static String sendPutRequest (String url, String jsonBody) throws Exception{
        CloseableHttpClient client = null;
        CloseableHttpResponse res = null;
        try {
            client = HttpClients.createDefault();
            RequestConfig requestConfig = RequestConfig.custom().
                    setSocketTimeout(5000).setConnectTimeout(5000).build();//设置请求和传输超时时间
            //创建一个PUT请求
            HttpPut put = new HttpPut(url);
            put.setConfig(requestConfig);
            StringEntity s = new StringEntity(jsonBody, "UTF-8");
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json;charset=UTF-8");
            put.setEntity(s);
            res = client.execute(put);
            
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                logger.info("sendPutRequest ok ");
                HttpEntity entity = res.getEntity();
                if (entity != null) {  
                    String resultStr = EntityUtils.toString(entity, "UTF-8");
                    logger.info(resultStr);
                    return resultStr;
                }  
            } else {
                HttpEntity entity = res.getEntity();
                if (entity != null) {  
                    String resultStr = EntityUtils.toString(entity, "UTF-8");
                    logger.info("sendPutRequest fail ");
                    logger.info(resultStr);
                    return resultStr;
                }  
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw e;
        } finally {
            if (res != null) {
                res.close();
            }
            if (client != null) {
                client.close();
            }
        }
        return null;
    }*/
}
